package org.vartist.atld.view.loading;

import org.vartist.atld.R;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.ImageView;

public class Loading extends Dialog {
	public static final int LOADING_DURATION = 100;
	LoadingCallback mParent;
	ImageView mLoadingImg;
	AnimationDrawable mAnimation;
	
	public Loading(Context context, LoadingCallback parent) {
		super(context);
		
		mParent = parent;
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
		setContentView(R.layout.activity_view_loading);
		
		mLoadingImg = (ImageView)findViewById(R.id.loading_img);
		
		mAnimation = new AnimationDrawable();
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading01), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading02), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading03), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading04), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading05), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading06), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading07), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading08), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading09), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading10), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading11), LOADING_DURATION);
		mAnimation.addFrame(context.getResources().getDrawable(R.drawable.icon_loading12), LOADING_DURATION);
		mAnimation.setOneShot(false);
		
		mLoadingImg.setBackgroundDrawable(mAnimation);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if(mAnimation == null)
			return;
		
		if(hasFocus)
			mAnimation.start();
		else
			mAnimation.stop();
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		
		mAnimation.stop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			mParent.onBackkeyDown();
			
//			dismiss();
			
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	public void destroyLoadingDialog(){
		freeImageView(mLoadingImg);
		if(mAnimation != null){
			mAnimation.stop();
			mAnimation.setCallback(null);
			mAnimation = null;
		}
	}
	
	private void freeImageView(ImageView iv) {
		if(iv != null){
			BitmapDrawable d = (BitmapDrawable)iv.getDrawable();
			if(d != null) {
//				d.getBitmap().recycle();
				d.setCallback(null);
			}
			iv.setImageBitmap(null);
			iv.setImageDrawable(null);
			iv.setBackgroundDrawable(null);
			iv.getResources().flushLayoutCache();
			iv.destroyDrawingCache();
		}
	}	
}
